﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;

namespace UserKTXD.TaiKhoan
{
    public partial class CNChuyenMuc : System.Web.UI.Page
    {
        HttpCookie cookie;
        protected void Page_Load(object sender, EventArgs e)
        {
            txtTieuDeTin.Focus();
            if (!Page.IsPostBack)
            {
                BindCatalog.BindLoaiChuyenMuc(cboLoaiChuyenMuc);
                BindCatalog.BindSubChuyenMuc(cboSubChuyenMuc);
                imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpAnhDaiDien + "/" + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
                if (Request.QueryString["id"] != null)
                {
                    H_CatalogId.Value = Request.QueryString["id"].ToString();
                    BindData(Convert.ToInt32(H_CatalogId.Value));
                }
                TList<SubChuyenMuc> TList_Catalogs = DataRepository.SubChuyenMucProvider.Find("IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue);
                cboSubChuyenMuc.DataSource = TList_Catalogs;
                cboSubChuyenMuc.DataTextField = "TenSubChuyenMuc";
                cboSubChuyenMuc.DataValueField = "Id";
                cboSubChuyenMuc.DataBind();
            }
        }



        protected void BindData(int IdTinTuc)
        {
            using (ChuyenMucCapHai entChuyenMucCapHai = DataRepository.ChuyenMucCapHaiProvider.GetById(IdTinTuc))
            {
                cookie = Request.Cookies.Get("UserNameAndPassword");
                if (entChuyenMucCapHai.IdTacGia == DataRepository.ThanhVienProvider.GetByTenTruyCap(cookie.Values["UserName"].ToString()).Id)
                {
                    txtTieuDeTin.Text = entChuyenMucCapHai.TieuDe;
                    cboLoaiChuyenMuc.SelectedValue = Convert.ToString(entChuyenMucCapHai.IdLoaiChuyenMuc);
                    cboSubChuyenMuc.SelectedValue = Convert.ToString(entChuyenMucCapHai.IdSubChuyenMuc);
                    chkTinNong.Checked = entChuyenMucCapHai.TrangThai;

                    //H_NgayCapNhat.Value = entChuyenMucCapHai.NgayCapNhat.ToString("dd/MM/yyyy");
                    H_AnhDaiDien.Value = entChuyenMucCapHai.AnhDaiDien;
                    imgViewer.ImageUrl = entChuyenMucCapHai.AnhDaiDien.ToString() != "no_image.jpg" ? Common.sPathUpAnhDaiDien + "/" + entChuyenMucCapHai.AnhDaiDien : "~/Images/no_image.jpg";
                    txtTomTat.Text = entChuyenMucCapHai.TomTat;
                    txtContent.Text = Server.HtmlDecode(entChuyenMucCapHai.NoiDung);
                    txtTag.Text = entChuyenMucCapHai.TagCloud;
                    H_TacGia.Value = entChuyenMucCapHai.IdTacGia.ToString();
                }
                else
                    Response.Redirect("~/");
            }
        }

        protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
        {
            bool updateStatus = false;
            try
            {
                cookie = Request.Cookies.Get("UserNameAndPassword");
                
                using (ChuyenMucCapHai entChuyenMuc = new ChuyenMucCapHai() { Id = Convert.ToInt32(H_CatalogId.Value), TieuDe = Common.StandardString(txtTieuDeTin.Text), TomTat = Common.StandardString(txtTomTat.Text), TrangThai = chkTinNong.Checked, NoiDung = Server.HtmlEncode(txtContent.Text), TagCloud = txtTag.Text, IdLoaiChuyenMuc = Convert.ToInt32(cboLoaiChuyenMuc.SelectedValue), IdSubChuyenMuc = Convert.ToInt32(cboSubChuyenMuc.SelectedValue), LuotXem = Convert.ToInt32(txtLuotXem.Text), IdTacGia = DataRepository.ThanhVienProvider.GetByTenTruyCap(cookie.Values["UserName"].ToString()).Id, NgayCapNhat = Common.GetServerDateTime() })
                {
                    imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpAnhDaiDien + H_AnhDaiDien.Value : "no_image.jpg";
                    if (H_AnhDaiDien.Value == "no_image.jpg")
                    {
                        entChuyenMuc.AnhDaiDien = "no_image.jpg";
                    }
                    else
                    {
                        entChuyenMuc.AnhDaiDien = H_AnhDaiDien.Value.Length > 0 ? H_AnhDaiDien.Value : "no_image.jpg";
                    }


                    if (entChuyenMuc.Id == 0)
                    {

                        updateStatus = DataRepository.ChuyenMucCapHaiProvider.Insert(entChuyenMuc);
                    }
                    else
                    {
                        entChuyenMuc.EntityState = EntityState.Changed;
                        updateStatus = DataRepository.ChuyenMucCapHaiProvider.Update(entChuyenMuc);
                    }

                    if (!updateStatus)
                        WebMessage.Show("Cập nhật Chuyên mục thất bại!");
                    else
                    {
                        Response.Redirect("~/TaiKhoan/QLChuyenMuc.aspx");
                        H_CatalogId.Value = entChuyenMuc.Id.ToString();
                    }

                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("CNChuyenMuc.aspx.cs", "cmdLuu_Click", ex.ToString());
                WebMessage.Show("Cập nhật Chuyên mục thất bại!");
            }
        }


        protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
        {
            H_CatalogId.Value = "0";
            H_AnhDaiDien.Value = "~/Images/no_image.jpg";
            txtTieuDeTin.Text = "";
            txtTomTat.Text = "";
            chkTinNong.Checked = true;
            txtContent.Text = "";
            txtTag.Text = "";
            cboLoaiChuyenMuc.SelectedValue = "0";
            cboSubChuyenMuc.SelectedValue = "0";
        }

        protected void cboLoaiChuyenMuc_Click(object sender, EventArgs e)
        {
            TList<SubChuyenMuc> TList_Catalogs = DataRepository.SubChuyenMucProvider.Find("IdLoaiChuyenMuc=" + cboLoaiChuyenMuc.SelectedValue);
            cboSubChuyenMuc.DataSource = TList_Catalogs;
            cboSubChuyenMuc.DataTextField = "TenSubChuyenMuc";
            cboSubChuyenMuc.DataValueField = "Id";
            cboSubChuyenMuc.DataBind();
        }
    }
}